﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace SC.CodeBuilder
{
    partial class Builder
    {
        #region 生成Model
        /// <summary>
        /// 生成Model
        /// </summary>
        /// <returns></returns>
        public StringBuilder BuildeModelSB()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("using System;");
            sb.AppendLine("using System.Collections.Generic;");
            sb.AppendLine("using System.Text;");
            sb.AppendLine();
            sb.Append("namespace ").Append(unameSpace).AppendLine("Model");
            sb.AppendLine("{");
            sb.AppendLine("    [Serializable]");
            BuildeModel(sb, Choice);
            return sb;
        }
        #endregion

        #region 根据用户设置生成代码
        /// <summary>
        /// 根据用户设置生成代码
        /// </summary>
        /// <param name="choice">用户设置组合</param>
        /// <returns></returns>
        private void BuildeModel(StringBuilder sb, int choice)
        {
            sb.Append("    public partial class ");
            switch (choice)
            {
                //泛型排序：IComparer<>, IComparable
                //字段比较：
                case 1:
                    sb.Append(tableName).AppendLine(hasidentity ? (" : IComparable<" + tableName + ">") : "");
                    sb.AppendLine("    {");
                    setAttribute(sb, tableName);
                    if (hasidentity) { setCompareTo(sb, identityName, true); }
                    sb.AppendLine("    }");
                    sb.AppendLine("}");
                    break;
                case 4:
                    sb.Append(tableName).AppendLine(hasidentity ? (" : IComparable<" + tableName + ">") : "");
                    sb.AppendLine("    {");
                    setAttribute(sb, tableName);
                    if (hasidentity) { setCompareTo(sb, identityName, true); }
                    setComparers(sb);
                    sb.AppendLine("    }");
                    sb.AppendLine("");
                    sb.AppendLine("}");
                    break;
                case 2:
                    sb.Append(tableName).AppendLine(hasidentity ? " : IComparable" : "");
                    sb.AppendLine("    {");
                    setAttribute(sb, tableName);
                    if (hasidentity) { setCompareTo(sb, identityName, false); }
                    sb.AppendLine("    }");
                    sb.AppendLine("}");
                    break;
                case 5:
                    sb.Append(tableName).AppendLine(hasidentity ? " : IComparable" : "");
                    sb.AppendLine("    {");
                    setAttribute(sb, tableName);
                    if (hasidentity) { setCompareTo(sb, identityName, false); }
                    setComparers(sb);
                    sb.AppendLine("    }");
                    sb.AppendLine("");
                    sb.AppendLine("}");
                    break;
                case 3:
                    sb.AppendLine(tableName);
                    sb.AppendLine("    {");
                    setAttribute(sb, tableName);
                    setComparers(sb);
                    sb.AppendLine("    }");
                    sb.AppendLine("");
                    sb.AppendLine("}");
                    break;
                case 6:
                    sb.AppendLine(tableName);
                    sb.AppendLine("    {");
                    setAttribute(sb, tableName);
                    sb.AppendLine("    }");
                    sb.AppendLine("}");
                    break;
            }
        }
        #endregion

        #region 根据当前存储的attName和attType生成所有字段的成员变量和公共属性
        /// <summary>
        /// 根据当前存储的attName和attType生成所有字段的成员变量和公共属性
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="tableName"></param>
        private void setAttribute(StringBuilder sb, string tableName)
        {
            sb.AppendLine("        #region 成员变量和公共属性");
            foreach (DataRow dr in Columns.Tables[0].Rows)
            {

                string dbType = sqlTypeToNetType(dr[5].ToString());
                string dr3 = dr[3].ToString();
                string dr3u = UpperLetter(dr3);

                sb.Append("        private ").Append(dbType).Append(" _").Append(dr3).AppendLine(";");
                sb.Append("        public ").Append(dbType).Append(" ").AppendLine(dr3u);
                sb.AppendLine("        {");
                sb.Append("            set { _").Append(dr3).AppendLine(" = value; }");
                sb.Append("            get { return _").Append(dr3).AppendLine("; }");
                sb.AppendLine("        }");
            }
            sb.AppendLine("        #endregion");
            sb.AppendLine("");
        }
        #endregion

        #region 生成泛型排序代码 - setCompareTo(StringBuilder sb, string lkeys, bool isList)
        /// <summary>
        /// 生成泛型排序代码
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="lkeys"></param>
        /// <param name="isList"></param>
        void setCompareTo(StringBuilder sb, string lkeys, bool isList)
        {
            sb.Append("        #region 实现").Append(isList ? "IComparable<T>" : "IComparable").Append("接口的").Append(isList ? "泛型" : null).AppendLine("排序方法");
            setAnnotate(sb, new string[] { "other" }, false, "根据" + UpperLetter(tableName) + "字段实现的" + (isList ? "IComparable<T>" : "IComparable") + "接口的" + (isList ? "泛型" : null) + "排序方法");
            sb.Append("        public int CompareTo(").Append(isList ? tableName : "object").AppendLine(" other)");
            sb.AppendLine("        {");
            sb.Append("            return ").Append(UpperLetter(lkeys)).Append(".CompareTo(");
            if (isList)//如果是泛型的
            {
                sb.Append("other.");
            }
            else//不是泛型
            {
                sb.Append("((").Append(tableName).Append(")other).");
            }
            sb.Append(UpperLetter(lkeys)).AppendLine(");");
            sb.AppendLine("        }");
            sb.AppendLine("        #endregion");
        }
        #endregion

        #region 根据字段生成比较器代码 - void setComparers(StringBuilder sb)
        /// <summary>
        /// 根据字段生成比较器代码
        /// </summary>
        /// <param name="sb"></param>
        private void setComparers(StringBuilder sb)
        {
            sb.AppendLine("    #region 实现IComparers<T>接口的泛型排序方法");
            foreach (DataRow dr in Columns.Tables[0].Rows)
            {
                string dr3 = dr[3].ToString();

                sb.AppendLine("    /// <sumary> ");
                sb.Append("    ///根据").Append(dr3).AppendLine("生成的升序比较器");
                sb.AppendLine("    /// </sumary> ");
                sb.AppendLine("    /// <returns></returns> ");
                sb.Append("    public class ").Append(UpperLetter(dr3)).Append("Comparer : IComparer<").Append(tableName).AppendLine(">");
                sb.AppendLine("    {");
                sb.Append("        public int Compare(").Append(tableName).Append(" x, ").Append(tableName).AppendLine(" y)");
                sb.AppendLine("        {");
                sb.Append("            return x.").Append(UpperLetter(dr3)).Append(".CompareTo(y.").Append(UpperLetter(dr3)).AppendLine(");");
                sb.AppendLine("        }");
                sb.AppendLine("    }");
                sb.AppendLine("");

                sb.AppendLine("    /// <sumary> ");
                sb.Append("    ///根据").Append(dr3).AppendLine("生成的降序比较器");
                sb.AppendLine("    /// </sumary> ");
                sb.AppendLine("    /// <returns></returns> ");
                sb.Append("    public class ").Append(UpperLetter(dr3)).Append("ComparerDesc : IComparer<").Append(tableName).AppendLine(">");
                sb.AppendLine("    {");
                sb.Append("        public int Compare(").Append(tableName).Append(" x, ").Append(tableName).AppendLine(" y)");
                sb.AppendLine("        {");
                sb.Append("            return y.").Append(UpperLetter(dr3)).Append(".CompareTo(x.").Append(UpperLetter(dr3)).AppendLine(");");
                sb.AppendLine("        }");
                sb.AppendLine("    }");
                sb.AppendLine("");
            }
            sb.AppendLine("    #endregion");
        }
        #endregion


        #region 生成注释代码 - void setAnnotate(StringBuilder sb, string[] paramNames, bool isClass, string classInfo)
        /// <summary>
        /// 根据参数名数组生成C#代码注释
        /// </summary>
        /// <param name="paramNames">参数名数组</param>
        /// <param name="count">缩进空格数</param>
        /// <returns></returns>
        void setAnnotate(StringBuilder sb, string[] paramNames, bool isClass, string classInfo)
        {
            sb.AppendLine("        /// <sumary> ");
            sb.Append("        /// ").AppendLine(classInfo);
            sb.AppendLine("        /// </sumary> ");
            if (!isClass)
            {
                foreach (string name in paramNames)
                {
                    sb.Append("        /// <param name=\"").Append(name).AppendLine("\"></param>");
                }
                sb.AppendLine("        /// <returns></returns> ");
            }
        }
        #endregion
    }
}
